package fun.codedesign.yinxue.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串处理工具类
 *
 * @author zengjian
 * @create 2018-04-11 17:11
 * @since 1.0.0
 */
public class StringUtil {

    /**
     * 字符串为空返回true
     *
     * @param str
     * @return true|false
     */
    public static boolean isEmpty(final String str) {
        return null == str || "".equals(str.trim());
    }

    /**
     * 字符串不为空返回true
     *
     * @param str
     * @return true|false
     */
    public static boolean isNotEmpty(final String str) {
        return !isEmpty(str);
    }

    /**
     * 首字母小写 <br>
     * 如: InnerName --> innerName;
     *
     * @param str
     * @return true|false
     */
    public static String toLowerCaseFirstChar(String str) {
        if (StringUtil.isEmpty(str)) {
            return "";
        }
        return str.substring(0, 1).toLowerCase().concat(str.substring(1));
    }

    /**
     * 首字母大写 <br>
     * 如: innerName --> InnerName;
     *
     * @param str
     * @return
     */
    public static String toUpperCaseFirstChar(String str) {
        return str.substring(0, 1).toUpperCase().concat(str.substring(1));
    }

    /**
     * @param text
     * @param oldChar
     * @param newChar
     * @return
     */
    public static String replace(String text, String oldChar, String newChar) {
        return text.replace(oldChar, newChar);
    }

    /**
     * 驼峰处理，以下划线后一位修改为大写为标准
     *
     * @param sqlFieldList
     * @return
     */
    public static String toCamelCase(final String sqlFieldList) {
        try {
            // 1.小写处理
            String lowerStr = sqlFieldList.toLowerCase();
            StringBuilder builder = new StringBuilder(lowerStr);
            Pattern p = Pattern.compile("_");
            Matcher m = p.matcher(builder);
            int index = 0;
            while (m.find()) {
                // end = 匹配组0的最后一个字符的索引+1
                index = m.end();
                // 2.获得下划线后的首字母，并作大写处理, 如果index是最后一个，则不用处理
                if (builder.length() == index) {
                    continue;
                }
                char c = Character.toUpperCase(builder.charAt(index));
                builder.setCharAt(index, c);
            }
            // 3.替换下划线为""
            return builder.toString().replaceAll("_", "");
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("sqlFieldList:" + sqlFieldList);
            return "";
        }
    }

    /**
     * 以大写字母为分隔，添加下划线
     *
     * @param javaField
     * @return
     */
    public static String toUnderLineCase(final String javaField) {
//        checkStringNull(javaField);
        StringBuilder builder = new StringBuilder(javaField);
        Pattern p = Pattern.compile("[A-Z]");
        Matcher m = p.matcher(builder);
        int index = 0;
        StringBuffer buffer = new StringBuffer();
        while (m.find()) {
            m.appendReplacement(buffer, "_" + m.group());
        }
        m.appendTail(buffer);
        return buffer.toString();
    }

}
